package com.ktjiaoyu.ch01_hello;

import com.ktjiaoyu.ch01_hello.pojo.Role;
import com.ktjiaoyu.ch01_hello.pojo.User;
import com.ktjiaoyu.ch01_hello.repository.RoleRepository;
import com.ktjiaoyu.ch01_hello.repository.UserRepository;
import com.ktjiaoyu.ch01_hello.service.UserService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class Ch01HelloApplicationTests2 {

    @Resource
    UserRepository userRepository;

    @Resource
    RoleRepository roleRepository;


    @Resource
    UserService userService;
    @Test
    void getUserAndRole(){
       /* List<User> all = userRepository.findAll();

        all.forEach(user -> {
            System.out.println(user);
        });*/

        //单向多对一  查到这个用户的时候立刻加载角色的数据
        User user = userRepository.findById(2L).get();
        System.out.println(user);
        System.out.println(user.getRole());
    }


    @Test
    void getRoles(){
//        Role role =roleRepository.findById(1L).get();
//
//        System.out.println(role);


        Role role =new Role();
        role.setRoleName("T365Admin");
        role.setRoleDesc("T365管理员");
        role.setRoleFlag("1");


        User user =new User();
        user.setUsrName("张昌");
        user.setUsrPassword("1234567");
        user.setUsrFlag(1);
        user.setRole(role);


        User user2 =new User();
        user2.setUsrName("周宏扬");
        user2.setUsrPassword("1234567");
        user2.setUsrFlag(1);
        user2.setRole(role);

        role.getUsers().add(user);
        role.getUsers().add(user2);

        roleRepository.save(role);

    }


    @Test
    void testCache(){
        User user = userService.findById(2L);
        System.out.println(user);
    }
}
